﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WEBWeixinSDK.WXMod;
using static WEBWeixinSDK.WXMod.WXMsg;

namespace WEBWeixinSDK.Plug
{
    public  class WXsql
    {
        /// <summary>
        /// 自动回复列表
        /// </summary>
        public static Dictionary<string, string> AllAutoMsg = new Dictionary<string, string>();
        /// <summary>
        /// 浏览器UserAgent
        /// </summary>
        public static Dictionary<int, string> AllUserAgent = new Dictionary<int, string>();
        /// <summary>
        /// Uin用户信息
        /// </summary>
        public static Dictionary<string,  string> AllWxuin=new Dictionary<string, string> () ;
        /// <summary>
        /// 自定义的消息保存函数
        /// </summary>
        /// <param name="_curMsg"></param>
        public static void DoSave(WXItinMsg _curMsg)
        {
            ///保存消息到本地的sqlite库
            SaveToSqlite(_curMsg);
        }

        /// <summary>
        /// 保存当前所给的消息到sqlite数据库中
        /// </summary>
        /// <param name="_curMsg"></param>
        private static void SaveToSqlite(WXItinMsg _curMsg)
        {
            SQLiteDBHelper db = new SQLiteDBHelper(Environment.CurrentDirectory + "\\wxbot.db");
          
            string sql = "INSERT INTO wxmsg(MSG_ID, MSG_FROM1, MSG_FROM2, MSG_CONTENT, MSG_TYPE, MSG_TIME, MSG_URL) VALUES (@id,@from1,@from2,@content,@type,@time,@url)";//私聊
            if (_curMsg.From2.Contains ("@@"))
            {
                 sql = "INSERT INTO wxgumsg(MSG_ID, MSG_FROM1, MSG_FROM2, MSG_CONTENT, MSG_TYPE, MSG_TIME, MSG_URL) VALUES (@id,@from1,@from2,@content,@type,@time,@url)";//群聊信息

            }
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                                                 new SQLiteParameter("@id",Guid.NewGuid().ToString("N")),
                                         new SQLiteParameter("@from1",_curMsg.From),
                                         new SQLiteParameter("@from2",_curMsg.From2),
                                         new SQLiteParameter("@content",_curMsg.Msg),
                                         new SQLiteParameter("@type",_curMsg.Type),
                                         new SQLiteParameter("@url",_curMsg.Url),
                                         new SQLiteParameter("@time",_curMsg.Time.ToString("yyyy-MM-dd HH:mm:ss.ffff"))
                                         };
            db.ExecuteNonQuery(sql, parameters);
        }
        /// <summary>
        /// 保存Uin用户数据WxUinBasics
        /// </summary>
        /// <param name="Uin"></param>
        /// <param name="UserName"></param>
        /// <param name="Cookite"></param>
        /// <param name="ToTime"></param>
        public static void SaveToWxuin(string Uin, string DeviceID, string UserAgent, DateTime ToTime)
        {
            SQLiteDBHelper db = new SQLiteDBHelper(Environment.CurrentDirectory + "\\wxbot.db");
            string sql = "INSERT INTO wxuin(Uin, DeviceID, UserAgent,ToTime) VALUES (@Uin,@DeviceID,@UserAgent,@ToTime)";
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                                                 new SQLiteParameter("@Uin",Uin.ToString()),
                                                 new SQLiteParameter("@DeviceID",DeviceID),
                                                        new SQLiteParameter("@UserAgent",UserAgent),
                                                 new SQLiteParameter("@ToTime",ToTime.ToString("yyyy-MM-dd HH:mm:ss.ffff")) };
            db.ExecuteNonQuery(sql, parameters);
        }
        /// <summary>
        /// 获取数据库信息
        /// </summary>
        /// <param name="sql"></param>
        public static void GetMsg(string table= "autoMsg")
        {
           string  sql = "select * from " + table;
            try
            {
                SQLiteDBHelper db = new SQLiteDBHelper(Environment.CurrentDirectory + "\\wxbot.db");
                using (SQLiteDataReader reader = db.ExecuteReader(sql, null))
                {
                   int i = 0;
                    while (reader.Read())
                    {
                        Dictionary<string, string> wxuin = new Dictionary<string, string>();
                        if (table.Trim() == "autoMsg")
                        {
                            if (!AllAutoMsg.ContainsKey(reader.GetString(0)))
                            {
                                AllAutoMsg.Add(reader.GetString(0), reader.GetString(1));

                            }
                        }
                        if (table.Trim() == "wxuin")
                        {
                           // = new Dictionary<string, Dictionary<string, string>>()
                            if (!AllWxuin.ContainsKey(reader.GetString(1)))
                            {
                                wxuin.Add(reader.GetString(2), reader.GetString(3));
                                AllWxuin.Add(reader.GetString(1), reader.GetString(2));

                            }
                        }
                        if (table.Trim() == "UserAgent")
                        {
                            if (!AllUserAgent.ContainsValue (reader.GetString(1)))
                            {
                                AllUserAgent.Add(i, reader.GetString(1));
                            }
                        }
                        i++;
                    }
                }
            }
            catch (Exception e)
            {
             ;
                Weixin.SendOnlog(e.Message.ToString());
               // throw;
            }
         //   return dTable;
        }
        /// <summary>
        /// 向数据库增加自动回复消息
        /// </summary>
        /// <param name="remsg">接收到的消息</param>
        /// <param name="sendmsg">要回复的消息</param>
        public static void InsertMsg(string remsg, string sendmsg)
        {
            SQLiteDBHelper db = new SQLiteDBHelper(Environment.CurrentDirectory + "\\wxbot.db");

            string sql = "INSERT INTO autoMsg(RE_MSG,SEND_MSG) VALUES (@re,@send)";

            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@re",remsg),
                new SQLiteParameter("@send",sendmsg)
            };
            db.ExecuteNonQuery(sql, parameters);
        }
    }
}
